using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;


namespace IcyTowerMobile
{
	/// <summary>
	/// Class representing the tower background.
	/// </summary>
	public class BackgroundBrick
	{

		private Texture2D texture;
		private Vector2[] positions;
		private Rectangle boundingBox;

		private int screenHeight;
		private int currentSpeed;
		private bool isLeft;

		public int Height
		{
			get { return texture.Height; }
		}

		public int Width
		{
			get { return texture.Width; }
		}

		public Rectangle BoundingBox
		{
			get { return boundingBox; }
		}

		public bool IsLeft
		{
			get { return isLeft; }
		}

		/// <summary>
		/// Allows the game component to perform any initialization it needs to before starting
		/// to run.  This is where it can query for any required services and load content.
		/// </summary>
		public void Initialize(ContentManager content, Texture2D texture, int screenHeight, int xPos, bool isLeft, int speed)
		{
			// Load the background texture we will be using 
			this.texture = texture;
			this.screenHeight = screenHeight;
			// If we divide the screen with the texture width then we can determine 
			// the number of tiles need. We add 2 to it so that we won't have a gap in the tiling 
			this.positions = new Vector2[screenHeight / texture.Height + 2];
			// Set the initial positions of the parallaxing background 
			for (int i = positions.Length - 1; i >= 0; i--)
			{
				// We need the tiles to be top on top to create a tiling effect 
				positions[i] = new Vector2(xPos, screenHeight - (positions.Length - i) * texture.Height);
			}
			this.boundingBox = new Rectangle(xPos, 0, texture.Width, screenHeight);
			this.isLeft = isLeft;
			this.currentSpeed = speed;
		}

		/// <summary>
		/// Allows the game component to update itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		public void Update(GameTime gameTime, int movement, bool playerIsHighEnough)
		{
			float min = positions.Min(position => position.Y) + movement;
			if (playerIsHighEnough)
			{
				// apply speed to brick if user has reached a certain height
				min += currentSpeed;
			}

			for (int i = 0; i < positions.Length; i++)
			{
				// Update the position of the screen by adding the speed
				positions[i].Y += movement;
				if (playerIsHighEnough)
				{
					// apply speed to brick if user has reached a certain height
					positions[i].Y += currentSpeed;
				}

				if (positions[i].Y >= screenHeight)
				{
					// set current texture back to top if it is not visible any more
					positions[i].Y = -texture.Height + min;
				}
			}
		}

		public void Draw(SpriteBatch spriteBatch)
		{
			// draw textures
			for (int i = 0; i < positions.Length; i++)
			{
				spriteBatch.Draw(texture, positions[i], Color.White);
			}
		}

		/// <summary>
		/// Increases game speed for this component by 1.
		/// </summary>
		public void increaseSpeed()
		{
			currentSpeed += 1;
		}
	}
}
